/**
 * @author        hejiyang <silentmanager@163.com>
 * @date          2024-11-16 21:08:28
 * Copyright © YourCompanyName All rights reserved
 */

import request from '@/config/axios'
import { ApiResponse } from '@/types/ref'

// 获得订单信息分页
export const pageOrderInfo = async (params) => {
  return await request.get<any>({ url: `/bms/order-info/page`, params })
}

// 删除订单信息
export const deleteOrderInfo = async (id: number) => {
  return await request.delete({ url: `/bms/order-info/delete?id=${id}` })
}

// 导出订单信息 Excel
export const exportExcelOrderInfo = async (params) => {
  return await request.download({ url: '/bms/order-info/export-excel', params })
}

// 获得订单信息
export const getOrderInfo = async (params) => {
  return await request.get<any>({ url: `/bms/order-info/get`, params })
}

// 批量审核
export const auditOrderInfo = async (data: { orderIds: number[]; auditResult: boolean }) => {
  return await request.request<ApiResponse<boolean>>({
    url: `/bms/order-info/audit`,
    data,
    method: 'POST'
  })
}

// 重新提交
export const reAuditOrderInfo = async (data: { orderIds: number[] }) => {
  return await request.request<ApiResponse<boolean>>({
    url: `/bms/order-info/re-audit`,
    data,
    method: 'POST'
  })
}

// 批量发货
export const deliveryOrderInfo = async (data: { orderIds: number[]; auditResult?: boolean }) => {
  return await request.request<ApiResponse<boolean>>({
    url: `/bms/order-info/delivery`,
    data,
    method: 'POST'
  })
}

export interface UpdateOrderInfoReq {
  id: number
  customerPhone: string
  customerName: string
  address: string
}
// 更新订单信息
export const updateOrderInfo = async (data: UpdateOrderInfoReq) => {
  return await request.request<ApiResponse<boolean>>({
    url: `/bms/order-info/update`,
    data,
    method: 'PUT'
  })
}

export interface SearchByProductRes {
  address: string // 地址
  age: number // 年龄
  contactName: string // 联系人姓名
  contactOnDelivery: boolean // 是否支持送货联系
  createTime: string // 创建时间
  creatorName: string // 创建人姓名
  customerName: string // 客户姓名
  customerPhone: string // 客户手机号
  customerType: string // 客户类型
  deliveryMethod: string // 配送方式
  department: string // 部门编号
  departmentName: string // 部门名称
  gender: string // 性别
  guaranteeAmount: number // 保证金额
  id: number // 唯一标识
  logisticStatus: number // 物流状态
  orderNo: string // 订单编号
  orderNote: string // 订单备注
  orderStatus: string // 订单状态
  packagePrepayment: number // 包裹预付款
  teamName: string // 班组名称
  updateTime: string // 更新时间
}

// 商品占用订单查询
export const searchByProduct = async (params: { drinkName: string }) => {
  return await request.get<{ list: SearchByProductRes[]; total: number }>({
    url: `/bms/order-info/search-by-product`,
    params
  })
}

// 创建订单申请解锁
export const requestUnLock = async (data: { customerId: string; customerMobile: string }) => {
  return await request.request<ApiResponse<boolean>>({
    url: `/system/notice/requestUnLock?customerId=${data.customerId}&customerMobile=${data.customerMobile}`,
    method: 'PUT',
    data
  })
}

// 七天下单解锁
export const unLockCustomer = async (id: string) => {
  return await request.request<ApiResponse<boolean>>({
    url: `/crm/customer/unLock?id=${id}`,
    method: 'PUT'
  })
}

// 驳回解锁
export const deleteNotice = async (id: string) => {
  return await request.request<ApiResponse<boolean>>({
    url: `/system/notify-message/update-read?ids=${id}`,
    method: 'PUT'
  })
}

interface OrderInfoTable {
  createTime: string
  id: number
  note: string
  orderId: number
  bottleCount: number //瓶数
  num: number //数量
  packageCount: number //箱数
  packageId: number //套餐ID
  packagePrice: number //套餐单价
  packageTotal: number //套餐总额
  packageTurnover: number //套餐营业额
  pendingPayment: number //待收尾款
  prepayment: number //预付款
  shippingExtra: number //运费加价
  packageExtra: number //套餐加价
  packageName: string //套餐名
}

export interface GetDetailOrderInfoRes {
  address: string //详细地址
  age?: number //年龄
  contactPerson?: string //对接人
  createTime: Date //创建时间
  customerName: string //客户姓名
  customerPhone: string //客户手机
  customerType?: string //客户类型
  deliveryMethod?: string //发货方式
  department?: string //对接部门
  gender?: string //性别
  id: number //编号
  logisticStatus?: number //物流状态
  orderNo: string //订单编号
  orderNote?: string //订单备注
  orderStatus?: string //订单状态
  packagePrepayment?: number //套餐预付款
  pendingPayment?: number //代收尾款
  team?: string //对接班组
  packageInfo: OrderInfoTable[]
  guaranteeAmount: string
  logisticNo: string
  teamName: string
  contactName: string
  departmentName: string
  creatorName: string
  contactTeamName: string
  contactDepartmentName: string
}
// 获得订单信息(详细)
export const getDetailOrderInfo = async (params: { id: number }) => {
  return await request.get<GetDetailOrderInfoRes>({
    url: `/bms/order-info/getDetail`,
    params
  })
}

// 创建订单备注
export const createOrderNote = async (data: { orderId: number; noteContent: string }) => {
  return await request.request<ApiResponse<boolean>>({
    url: '/bms/order-note/create',
    method: 'POST',
    data
  })
}

// 获得订单备注分页
export const pageOrderNote = async (params: {
  orderId: number
  pageNo: number
  pageSize: number
}) => {
  return await request.get({
    url: `/bms/order-note/page`,
    params
  })
}

// 删除订单备注
export const deleteOrderNote = async (params: { id: number }) => {
  return await request.delete({ url: `/bms/order-note/delete`, params })
}
